<template>
 <el-button type="danger" @click="deleteFiles">删除</el-button>
</template>

<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus'
import { fileStore} from '@/stores/disk/file'
import {deleteFileToRecycle,deleteFilesFinish} from '@/api/disk/file'
const store= fileStore();
const deleteFiles = () => {
  const List = store.state.deleteList
  if (List.length <= 0) {
    ElMessage.warning('请先选择要删除的文件')
    return
  }
  const index= store.state.currentMenuIndex
  //判断索引
  if(index===9){
    //当前在回收站
    ElMessageBox.confirm(
      '此操作将永久删除该文件, 是否继续?',
      'Warning',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        title: '提示',
        type: 'warning',
      }
    ).then(() => {
      //执行请求
      if(store.state.currentBread.length>0){
        ElMessage.error('暂不支持单独删除子级文件,请将整个文件夹一起删除')
        return
      }
      deleteFilesFinish(List).then(() => {
        //删除成功
        store.resetFileList()
        //把删除列表清空
        store.updateDeleteList([])
      })
    }).catch(() => {
      //取消删除
      ElMessage.info('已取消删除')
    })
  }else{
    //不在回收站
    ElMessageBox.confirm(
      '此操作将删除该文件到回收站, 是否继续?',
      'Warning',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        title: '提示',
        type: 'warning',
      }
    ).then(() => {
      //执行请求
      try {
        deleteFileToRecycle(List).then(() => {
          //删除成功
          store.resetFileList()
          //把删除列表清空
          store.updateDeleteList([])
        })
      } catch (error) {
        console.log(error)
        ElMessage.error('永久删除失败')
      }
      
    }).catch(() => {
      //取消删除
      ElMessage.info('已取消删除')
    })
  }

}
</script>

<style scoped>

</style>